package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.filecenter.domain.BusinessConstants;
import com.bwda.dsrs.filecenter.domain.po.keyrule.KeyRulePo;
import com.bwda.dsrs.filecenter.domain.condition.keyrule.KeyRuleCondition;
import com.bwda.dsrs.filecenter.domain.form.keyrule.KeyRuleQueryForm;
import com.bwda.dsrs.filecenter.domain.form.keyrule.KeyRuleUpdateForm;
import com.bwda.dsrs.filecenter.domain.form.keyrule.KeyRuleCreateForm;
import com.bwda.dsrs.filecenter.domain.po.registerkey.RegisterKeyPo;
import com.bwda.dsrs.filecenter.domain.vo.keyrule.KeyRuleVo;
import com.bwda.dsrs.filecenter.service.KeyRuleService;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.filecenter.service.RegisterKeyService;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang.math.RandomUtils;
/**
 * @author Administrator
 */
@RestController
@RequestMapping("/b/keyrule")
@Api(value = "/b/keyrule", tags = "秘钥规则")
public class KeyRuleController extends BaseController {

	@Autowired
	private KeyRuleService keyRuleService;
	@Autowired
	private RegisterKeyService registerKeyService;

	@ApiOperation(value = "查询秘钥规则",notes = "查询秘钥规则",httpMethod = "GET")
	@RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<KeyRuleVo> query(@ApiParam(value = "秘钥规则id", required = true)@RequestParam Integer keyRuleId) throws BusinessServiceException {
		KeyRulePo po = keyRuleService.queryWithValid(keyRuleId);
		KeyRuleVo vo = CopyUtil.transfer(po, KeyRuleVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "查询秘钥规则数量",notes = "查询秘钥规则数量",httpMethod = "GET")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid KeyRuleQueryForm form) throws BusinessServiceException {
		KeyRuleCondition condition = this.getConditionByQueryForm(form);
		condition.setDelFlag(BusinessConstants.NumberConstant.ZERO);
		int count = keyRuleService.queryCount(condition);
		return getSuccessResult(count);
	}

	@ApiOperation(value = "查询秘钥规则列表",notes = "查询秘钥规则列表",httpMethod = "GET")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<KeyRuleVo>> queryList(@ModelAttribute@Valid KeyRuleQueryForm form) throws BusinessServiceException {
		KeyRuleCondition condition = this.getConditionByQueryForm(form);
		condition.setDelFlag(BusinessConstants.NumberConstant.ZERO);
		List<KeyRulePo> poList = keyRuleService.queryList(condition);
		List<KeyRuleVo> voList = CopyUtil.transfer(poList, KeyRuleVo.class);
		return getSuccessResult(getListResponse(voList));
	}

	@ApiOperation(value = "查询秘钥规则列表(带分页)",notes = "查询秘钥规则列表(带分页)",httpMethod = "GET")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<KeyRuleVo>> queryPageList(@ModelAttribute@Valid KeyRuleQueryForm form) throws BusinessServiceException {
		KeyRuleCondition condition = this.getConditionByQueryForm(form);
		List<KeyRuleVo> voList = new ArrayList<>();
		List<KeyRulePo> poLists = new ArrayList<KeyRulePo>();
        condition.setDelFlag(BusinessConstants.NumberConstant.ZERO);
		int count = keyRuleService.queryCount(condition);
		if (count > BusinessConstants.NumberConstant.ZERO) {
			List<KeyRulePo> poList = keyRuleService.queryList(condition);
			if(poList!=null && poList.size()>0){
				for(KeyRulePo po:poList){
					Integer keyRuleId = po.getKeyRuleId();
					List<RegisterKeyPo> registerKeyPoList=registerKeyService.queryRegisterKeyList(keyRuleId);
			       if(registerKeyPoList!=null && registerKeyPoList.size() > 0){
					   po.setStatusFlag(1);
			       }
					poLists.add(po);
				}
			}
			voList = CopyUtil.transfer(poLists, KeyRuleVo.class);
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}

	@ApiOperation(value = "新增秘钥规则",notes = "新增秘钥规则",httpMethod = "POST")
	@RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<KeyRuleVo> add(@ModelAttribute@Valid KeyRuleCreateForm form) throws BusinessServiceException {
		KeyRulePo po = CopyUtil.transfer(form, KeyRulePo.class);//lombok
//		po.setKeyRuleId(RandomUtils.nextInt());
		//默认状态：未使用
		po.setStatusFlag(0);
		//默认未删除
		po.setDelFlag(0);
		po.setCreateTime(new Date());
		po.setUpdateTime(new Date());
        boolean isExist= keyRuleService.isKeyRuleNameUnique(po.getRuleName());
        if(isExist==false){
            return getFailResult("密钥规则名称重复，请重新输入密钥规则名称!");
        }
		keyRuleService.insert(po);
		KeyRuleVo vo = CopyUtil.transfer(po, KeyRuleVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "修改秘钥规则",notes = "修改秘钥规则",httpMethod = "POST")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid KeyRuleUpdateForm form) throws BusinessServiceException {
		KeyRulePo po = CopyUtil.transfer(form, KeyRulePo.class);
		//修改时间
		po.setUpdateTime(new Date());
		keyRuleService.update(po);
		return getSuccessResult();
	}

	@ApiOperation(value = "删除秘钥规则",notes = "删除秘钥规则",httpMethod = "POST")
	@RequestMapping(value = "/deleteByIds", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "秘钥规则id", required = true)@RequestParam List<Integer> keyRuleIds) throws BusinessServiceException {
        logger.info("删除秘钥规则");
        //支持批量
        for(Integer keyRuleId:keyRuleIds){
			List<RegisterKeyPo> registerKeyPoList=registerKeyService.queryRegisterKeyList(keyRuleId);
			if(registerKeyPoList!=null && registerKeyPoList.size() > 0){
				KeyRulePo keyRulePo = keyRuleService.query(keyRuleId);
				keyRulePo.setStatusFlag(1);
				return getFailResult("密钥规则已被绑定不允许删除");
			}
			keyRuleService.delete(keyRuleId);
		}
		return getSuccessResult();
	}

	/**
	 * KeyRuleQueryForm转换为KeyRuleCondition
	 *
	 * @param form
	 * @return
	 */
	private KeyRuleCondition getConditionByQueryForm(KeyRuleQueryForm form) {
		KeyRuleCondition condition = CopyUtil.transfer(form, KeyRuleCondition.class);
		return condition;
	}

}